<template>
  <el-dialog
    title="需要您的身份认证"
    :visible.sync="IdentifyDialogVisible"
    width="35%"
    :before-close="handleClose"
    id="accountDialog"
  >
    <div class="uInfo-msg-content center">
      <span class="uInfo-text-container"
        ><span class="uInfo-vertical-align"
          >为确认您本人操作，请进行身份认证</span
        ></span
      >
    </div>
    <el-form :rules="SmsRules" :model="SmsForm" ref="SmsForm" status-icon>
      <el-form-item
        label=""
        prop="phone"
        v-if="SmsState.phoneState"
        :error="SmsForm.phoneErrormsg"
      >
        <div class="uInfo-input-right">
          <input
            type="button"
            id="getValiCode"
            value="获取验证码"
            class="uInfo-get-code uInfo-verificationDialogCodeBtn"
            @click="Bgetsmscode()"
          />
        </div>
        <el-input
          type="text"
          v-model="SmsForm.phone"
          autocomplete="off"
          placeholder="电话号码"
        >
        </el-input>
      </el-form-item>
      <el-form-item
        label=""
        prop="smscode"
        v-if="SmsState.smscodeState"
        :error="SmsForm.smscodeErrormsg"
      >
        <div class="uInfo-input-right">
          <span v-if="SmsState.timeState">{{ SmsForm.second }}s</span>
          <input
            type="button"
            id="getValiCode"
            value="重新发送"
            class="uInfo-get-code uInfo-verificationDialogCodeBtn"
            v-if="SmsState.timeoutState"
            @click="Agetsmscode()"
          />
        </div>
        <el-input
          v-model="SmsForm.smscode"
          placeholder="请输入验证码"
        ></el-input>
      </el-form-item>
    </el-form>
    <div class="f-rules center">
      <div class="f-rules-title">
        <span class="uInfo-text-container"
          ><span class="uInfo-vertical-align">手机号无法使用？</span></span
        >
        <span>账号申诉</span>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="sendsms()">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { validatePhone } from "../../plugins/validator";
export default {
  data() {
    var validateSmscode = (rule, value, callback) => {
      let reg = /^\d{6}$/;
      if (!value) {
        callback(new Error("请输入验证码"));
      } else if (!reg.test(value)) {
        callback(new Error("请输入6位数字验证码"));
      } else {
        callback();
      }
    };
    return {
      IdentifyDialogVisible: true,
      SmsForm: {
        phone: "",
        smscode: "",
        second: 60,
        sendTime: "",
        trimer: "",
        phoneErrormsg: "",
        smscodeErrormsg: "",
      },
      SmsState: {
        phoneState: true,
        smscodeState: false,
        timeState: true,
        timeoutState: false,
        idefiniTime: "",
      },
      SmsRules: {
        phone: [{ require: true, validator: validatePhone, triiger: "blur" }],
        smscode: [
          { require: true, validator: validateSmscode, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    handleClose() {
      this.$emit("changeSmsstate");
      this.closestate();
    },
    closeDialog() {
      this.$emit("changeSmsstate");
      this.closestate();
    },
    //关闭窗口修改状态
    closestate() {
      this.SmsState = {
        phoneState: true,
        smscodeState: false,
        timeState: true,
        timeoutState: false,
        idefiniTime: "",
      };
      this.SmsForm.second = 60;
      clearInterval(this.SmsForm.trimer);
    },
    //发送短信
    Bgetsmscode() {
      this.$refs.SmsForm.validate(async (valid) => {
        if (!valid) return false;
        else {
          this.getsmscode()
            .then((e) => {
              this.$message({
                type: "success",
                message: `您的短信验证码为${e}`,
              });
              this.SmsState.phoneState = false;
              this.SmsState.smscodeState = true;
              this.SmsForm.trimer = setInterval(this.TimeTrimer, 1000);
            })
            .catch(() => {
              this.SmsForm.phoneErrormsg = "您输入的电话号码错误";
            });
        }
      });
    },
    Agetsmscode() {
      this.getsmscode()
        .then((e) => {
          this.$message({
            type: "success",
            message: `您的短信验证码为${e}`,
          });
          this.SmsState.phoneState = false;
          this.SmsState.smscodeState = true;

          this.SmsForm.trimer = setInterval(this.TimeTrimer, 1000);
        })
        .catch(() => {
          this.SmsForm.phoneErrormsg = "您输入的电话号码错误";
        });
    },
    TimeTrimer() {
      this.SmsForm.second--;
      if (this.SmsForm.second == 0) {
        this.SmsState.timeState = false;
        this.SmsState.timeoutState = true;
        clearInterval(this.SmsForm.trimer);
        // this.SmsForm.trimer = ""
        this.SmsForm.second = 60;
      }
    },
    getsmscode() {
      return new Promise((resolve, reject) => {
        let userPhone = {
          phone: this.SmsForm.phone,
          _id: this.$store.getters.UserInfoID,
          sendtime: Date.now(),
        };
        this.axios
          .post("/user/IdentifyPhone", userPhone)
          .then((res) => {
            if (res.data.data.code == 200) {
              resolve(res.data.data.smscode);
            } else {
              reject(res.data.data.code);
            }
          })
          .catch((err) => {
            this.$message.error("出错了哦");
            reject(err);
          });
      });
    },
    sendsms() {
      this.$refs.SmsForm.validate(async (valid) => {
        if (!valid) return false;
        else {
          let userSmscode = {
            _id: this.$store.getters.UserInfoID,
            phone: this.SmsForm.phone,
            nowtime: Date.now(),
            smscode: this.SmsForm.smscode,
          };
          this.axios
            .post("/user/IdentifySmscode", userSmscode)
            .then((res) => {
              if (res.data.data.code == 200) {
                this.$emit("hh");
              } else if (res.data.data.code == 500) {
                this.SmsForm.smscodeErrormsg = "错误的验证码";
              } else if (res.data.data.code == 501) {
                this.SmsForm.smscodeErrormsg = "验证码已过期";
              } else if (res.data.data.code == 502) {
                this.SmsForm.smscodeErrormsg = "未知的错误";
              }
            })
            .catch((err) => {
              console.log(err);
              this.$message.error("出错了哦");
              return false;
            });
        }
      });
    },
  },
};
</script>

<style scoped>
.uInfo-input-right {
  position: absolute;
  line-height: 48px;
  height: 100%;
  right: 64px;
  top: 22px;
  z-index: 999;
}

.uInfo-get-code {
  border: none;
  cursor: pointer;

  background-color: transparent;
  display: inline-block;
  height: 20px;
  color: #007dff;
  font-size: 14px;
}

#verificationCodeCtr .uInfo-verificationDialogCodeBtn {
  padding: 0 16px;
  line-height: 48px;
}

#clearfix::after {
  /* 添加内容 */
  content: "";
  /* 转块元素 */
  display: block;
  /* 清除浮动 */
  clear: both;
  visibility: hidden;
}

.center {
  text-align: center;
}
</style>